<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出登录</el-button>
        </el-header>
        <el-container>
            <el-aside :width="isColl? '64px' : '200px'">
                <div class="toggle_btn" @click="toggleMenu">|||</div>
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened
                 :collapse="isColl" :collapse-transition="false" router :default-active="theAcitve">
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.path" v-for="item in menuLists" :key="item.id">
                        <template slot="title">
                            <!-- 菜单图标 -->
                            <i :class="iconList[item.id]"></i>
                            <!-- 菜单文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                            <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children"
                            :key="subitem.id" @click="activeMenu('/' + item.path)">
                                <!-- 菜单图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 菜单文本 -->
                                <span>{{subitem.authName}}</span>
                            </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view />
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    created() {
        this.getMenuLists()
        this.theAcitve = window.sessionStorage.getItem('activeMenu')
    },
    data() {
        return {
            menuLists: [],
            isColl: false,
            iconList: {
                '125': 'iconfont icon-user',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpin',
                '102': 'iconfont icon-danju',
                '145': 'iconfont icon-baobiao'
            },
            theAcitve: ''
        }
    },
    methods: {
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        async getMenuLists() {
            const { data } = await this.$http.get('menus')
            // 打印了获取到的菜单数据
            // console.log(data)
            // ---------end--------
            this.menuLists = data.data
        },
        toggleMenu() {
            this.isColl = !this.isColl
        },
        activeMenu(menu) {
            window.sessionStorage.setItem('activeMenu', menu)
            this.theAcitve = menu
        }
    }
}
</script>

<style lang="less" scoped>
    .home-container {
        height: 100%;
    }

    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 20px 0 0;
        color: #fff;
        font-size: 20px;
        > div {
            display: flex;
            align-items: center;
            span {
                margin-left: 15px;
            }
        }
    }

    .el-aside {
        background-color: #333744;
        .toggle_btn {
            background-color: #4a5064;
            color: #fff;
            text-align: center;
            line-height: 24px;
            font-size: 10px;
            cursor: pointer;
            letter-spacing: 0.2em;
        }
        .el-menu {
            border: none;
        }
    }

    .el-main {
        background-color: #eaedf1;
    }

    .iconfont {
        padding-right: 10px;
    }

</style>